define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'general/links/index',
                    add_url: 'general/links/add',
                    edit_url: 'general/links/edit',
                    del_url: 'general/links/del',
                    multi_url: 'general/links/multi',
                    table: 'links'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: "名称"},
                        {field: 'position', title: "显示位置",formatter: Controller.api.formatter.position},
                        {field: 'weigh', title: "权重", sortable: true},
                        {field: 'url_link', title: "链接地址"},
                        {field: 'icon_link', title: "图标地址"},
                        {field: 'createtime', title: "创建时间", formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'status', title: __("Status"), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                position: function (value, row, index) {
                    if (row.position ==1) {
                        return "首页";
                    } 
                    if (row.position ==2) {
                        return "图书馆";
                    } 
                    return value;
                }
            }
        }
    };
    return Controller;
});
